package ai.peanut.selfbms.controller.sys;


import ai.peanut.selfbase.common.domain.DataResult;
import ai.peanut.selfbase.common.enums.ResultTypeEnum;
import ai.peanut.selfbase.common.utils.UserUtils;
import ai.peanut.selfbase.modules.menu.entity.Menu;
import ai.peanut.selfbase.modules.menu.pojo.MenuDto;
import ai.peanut.selfbase.modules.menu.pojo.MenuParams;
import ai.peanut.selfbase.modules.menu.pojo.MenuTreeDto;
import ai.peanut.selfbase.modules.sys.pojo.ZTreeDto;
import ai.peanut.selfbase.modules.menu.service.IMenuService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 菜单 前端控制器
 * </p>
 *
 * @author lichong
 * @since 2019-02-15
 */
@Controller
@Slf4j
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private IMenuService menuService ;

    @RequiresPermissions("menu:menuIndex")
    @RequestMapping(value = "/menuIndex", method = RequestMethod.GET)
    @ResponseBody
    public DataResult<List<MenuTreeDto>> menuIndex(HttpServletRequest request, HttpServletResponse response){
        try {
            List<MenuTreeDto> list = menuService.selectMenuTreeListForIndex() ;
            return  DataResult.success( list ,(Class<List<MenuTreeDto>>)list.getClass()) ;
        }catch ( Exception e ){
            log.error("cps商品列表 管理后台 列表异常:" + e,e);
        }
        return DataResult.faild(ResultTypeEnum.FAILED.getCode(),
                ResultTypeEnum.FAILED.getMessage());
    }


    @RequiresPermissions("menu:getMenuTreeListByRoleId")
    @RequestMapping(value = "/getMenuTreeListByRoleId", method = RequestMethod.GET)
    @ResponseBody
    public DataResult<List<ZTreeDto>> getMenuTreeListByRoleId(Long roleId , HttpServletRequest request, HttpServletResponse response){
        try {
            List<ZTreeDto> list = menuService.selectMenuZTreeListByRoleId(roleId) ;
            return  DataResult.success( list ,(Class<List<ZTreeDto>>)list.getClass()) ;
        }catch ( Exception e ){
            log.error("cps商品列表 管理后台 列表异常:" + e,e);
        }
        return DataResult.faild(ResultTypeEnum.FAILED.getCode(),
                ResultTypeEnum.FAILED.getMessage());
    }

    @RequiresPermissions("menu:getMenuTreeNotInIconList")
    @RequestMapping(value = "/getMenuTreeNotInIconList", method = RequestMethod.GET)
    @ResponseBody
    public DataResult<List<ZTreeDto>> getMenuTreeNotInIconList(Long id , HttpServletRequest request, HttpServletResponse response){
        try {
            List<ZTreeDto> list = menuService.getMenuTreeNotInIconList(id) ;
            return  DataResult.success( list ,(Class<List<ZTreeDto>>)list.getClass()) ;
        }catch ( Exception e ){
            log.error("cps商品列表 管理后台 列表异常:" + e,e);
        }
        return DataResult.faild(ResultTypeEnum.FAILED.getCode(),
                ResultTypeEnum.FAILED.getMessage());
    }


    @RequiresPermissions("menu:list")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public List<Menu> list(HttpServletRequest request, HttpServletResponse response){
        try {
            MenuParams params = new MenuParams() ;
            List<Menu> list = menuService.selectAllByParams(params) ;
            return  list ;
        }catch ( Exception e ){
            log.error("cps商品列表 管理后台 列表异常:" + e,e);
        }
        return null ;
    }

    @RequiresPermissions("menu:info")
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    @ResponseBody
    public DataResult<MenuDto> info(Long id ,HttpServletRequest request, HttpServletResponse response){
        try {
            Menu menu  = menuService.selectById(id) ;
            MenuDto dto = new MenuDto() ;
            BeanUtils.copyProperties( menu , dto ) ;
            if(menu != null && menu.getParentId().intValue() != 0 ) {
                Menu m = menuService.selectById(menu.getParentId()) ;
                if( m != null ) {
                    dto.setParentName(m.getName()) ;
                }
            }
            return  DataResult.success(dto , MenuDto.class) ;
        }catch ( Exception e ){
            log.error("cps商品列表 管理后台 列表异常:" + e,e);
        }
        return DataResult.faild(ResultTypeEnum.FAILED.getCode(),
                ResultTypeEnum.FAILED.getMessage());
    }


    @RequiresPermissions("menu:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public DataResult<Boolean> saveOrUpdate(@RequestBody MenuParams params, HttpServletRequest request, HttpServletResponse response) {
        try {
            if (params.getId() == null) {
                return menuService.insertMenu(params);
            } else {
                return menuService.updateMenu(params);
            }
        } catch (Exception e) {
            log.error("cps商品修改 管理后台 :", e);
        }
        return DataResult.faild(ResultTypeEnum.FAILED.getCode(),
                ResultTypeEnum.FAILED.getMessage());
    }


    @RequiresPermissions("menu:delete")
    @RequestMapping("/delete")
    @ResponseBody
    public DataResult<Boolean> delete(@RequestBody MenuParams params) {
        try {
            log.info("cps商品删除 管理后台 :{}", "请求参数:" + params.toString());
            if( params.getId() == null ){
                return DataResult.faild(ResultTypeEnum.ID_NULL.getCode(),
                        ResultTypeEnum.ID_NULL.getMessage());
            }
            params.setUpdateUserId(UserUtils.getCurrentUser().getId());
            params.setDelFlag(0);
            int deNum = menuService.deleteCurrentAndChildren(params);
            return DataResult.success();
        } catch (Exception e) {
            log.error("cps商品删除 管理后台 :", e);
        }
        return DataResult.faild(ResultTypeEnum.FAILED.getCode(),
                ResultTypeEnum.FAILED.getMessage());

    }






}

